﻿@using Think9.Models;
@model Think9.Models.ExtraDbEntity
@{ ViewBag.Title = "Edit";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<form class="layui-form" lay-filter="formEdit">
    <input style="display:none" id="DbID" name="DbID">
    <div class="layui-form-item">
        <label class="layui-form-label required">数据库类别</label>
        <div class="layui-input-block">
            <select id="DbType" name="DbType" lay-filter="selectDbType" lay-verify='required'>
                @foreach (valueTextEntity item in ((IEnumerable
                <valueTextEntity>
                )ViewBag.TypeList))
                {
                    <option value="@item.Value">@item.Text</option>
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">数据库名称</label>
        <div class="layui-input-block">
            <input type="text" name="DbName" id="DbName" placeholder="请输入数据库名称 类别为MySQL、Oracle数据库时需与数据库名相同" autocomplete="off" class="layui-input" lay-verify="required" maxlength="50">
            <span style="color: #FE7300;">⇑类别为MySQL、Oracle数据库时需与数据库名相同</span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">连接字符</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入连接字符" value="" name="DbCon" id="DbCon" class="layui-textarea" lay-verify="required"></textarea>
            <span id="spanDbCon" style="color:darkgrey">格式：server=serverName(如：127.0.0.1);port=3306;database=databaseName(如：yourslc);user=username(如：root);password=password(如：123456);</span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入备注" value="" name="Remarks" id="Remarks" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add">编辑</button>
            <button class="layui-btn" lay-submit lay-filter="connection">测试连接</button>
        </div>
    </div>
</form>
<script>
    layui.use(["form", "exLayer", "exUtils"], function () {
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;

        let $ = layui.$;

        form.render(); //

        //赋值
        form.val("formEdit", {
            "DbType": "@Model.DbType",
            "DbName": "@Model.DbName",
            "DbCon": "@Model.DbCon",
            "Remarks": "@Model.Remarks",
            "DbID": "@Model.DbID"
        });

        form.on("submit(add)", function (data) {
            exUtils.ajax("/SysBasic/ExternalDb/Edit", "post", { model: data.field, id: $("#DbID").val() }, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    parent.layui.table.reload('tableId', { url: "/SysBasic/ExternalDb/GetList" });
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        form.on("submit(connection)", function (data) {
            if ($("#DbCon").val() == '') {
                layer.msg('请输入连接字符');
                return false;
            }
            exUtils.ajax("/SysBasic/ExternalDb/Connection", "post", data.field, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {

                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        form.on('select(selectDbType)', function (data) {
            if (data.value == 'mysql') {
                $("#spanDbCon").html("格式：server=serverName(如：127.0.0.1);port=3306;database=databaseName(如：yourslc);user=username(如：root);password=password(密码);");
            }
            if (data.value == 'sqlserver') {
                $("#spanDbCon").html("格式：Server=serverName(如：127.0.0.1);Database=databaseName(如：yourslc);User Id=username(如：sa);Password=password(密码); Timeout=30;");
            }
            if (data.value == 'postgresql') {
                $("#spanDbCon").html("格式：Host=serverName(如：127.0.0.1);Port=5432;Database=databaseName(如：yourslc);Username=username(如：postgres);Password=password(密码); Timeout=30;");
            }
            if (data.value == 'oracle') {
                $("#spanDbCon").html("格式：Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST=serverName(如：127.0.0.1) (PORT=1521)))(CONNECT_DATA=(SERVICE_NAME= orcl)));User Id=用户名(如：***); Password=密码(如：***);");
            }
        })
    });
</script>